﻿<%@ WebHandler Language="C#" Class="sysMenuHandler" %>

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;

using log4net;
using log4net.Config;

using Web.Library.DomainObject.SQLServer;
using Web.Library.Facade.SQLServer;
using Web.Library.DataAccess.SQLServer;
using Web.Support;



public class sysMenuHandler : BaseHandler {
    private  SysMenuFacade Facade = new SysMenuFacade(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    public override void ProcessRequest(HttpContext context)
    {
        base.ProcessRequest(context);
        context.Response.ContentType = "text/plain";
        string result = this.Process(context);
        context.Response.Write(result);
    }

    private string Process(HttpContext context)
    {
        string result = string.Empty;
        try
        {
            context.Response.Clear();
            context.Response.Buffer = true;

            System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream);

            string jsonString = string.Empty;
            jsonString = context.Server.UrlDecode(sr.ReadToEnd());
            JObject jObj = string.IsNullOrEmpty(jsonString) ? null : JObject.Parse(jsonString);

            sr.Close();
            sr.Dispose();
            
            if (context.Request.QueryString["show"] != null)
            {
                switch (context.Request.QueryString["show"].ToString())
                {
                    case "list": result = this.MenuList(context); break;
                    //case "formload": result = this.FormLoad(context); break;
                    case "create": result = this.Create(context, jObj); break;
                    case "update": result = this.Update(context,jObj); break;
                    //case "delete": result = this.Delete(context); break;
                }
            }

            if (context.Request.QueryString["type"] != null)
            {
                switch (context.Request.QueryString["type"].ToString())
                {
                    case "combo": result = this.ComboBox(context); break;
                    case "delete": result = this.Delete(context); break;
                }
            }
        }
        catch (Exception ex)
        {
            //result = "{success: false, message: 'Error found on page! " + ConfigurationManager.AppSettings["ErrorHelpText"].ToString() + "'}";
            //log.Debug("Process() on MenuHandler.ashx.cs: " + ex.Message.ToString());

            Logger.Debug("Process () sysMenuHandler.ashx", ex);
        }
        return result;
    }

    private string Update(HttpContext context, JObject jo)
    {
        string result = string.Empty;
        JSONHelper helper = new JSONHelper();
        try
        {
            SysMenu obj = new SysMenu();
            obj.MenuID = (int)jo["MenuID"];
            obj.Title = (string)jo["Title"];
            obj.Url = (string)jo["Url"];
            obj.Sort = (short)jo["Sort"];
            obj.ParentID = (int)jo["ParentID"];
            obj.UpdatedBy = base.UserLoggedIn;
            obj.UpdatedDate = DateTime.Now;
            
            Facade.Update(obj);
            //result = "{\"success\": \"true\"}";

            helper.Message = "Data berhasil di simpan !";
            helper.IsSuccess = true;
        }
        catch (Exception ex)
        {
            helper.Message = ex.Message;
            helper.IsSuccess = false;
            throw ex;
        }

        //return result;
        return Newtonsoft.Json.JsonConvert.SerializeObject(helper);
    }

    private string Delete(HttpContext context)
    {
        string result = string.Empty;
        try
        {

            Facade.Delete(Convert.ToInt32(context.Request.QueryString["Id"].ToString()));
            result = "{\"success\": \"true\"}";
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

    private string Create(HttpContext context, JObject jo)
    {
        string result = string.Empty;
        JSONHelper helper = new JSONHelper();
        try
        {

            SysMenu obj = new SysMenu();
            obj.Title = (string)jo["Title"];
            obj.Url = (string)jo["Url"];
            obj.ParentID = (int)jo["ParentID"];
            obj.Sort = (short)jo["Sort"];
            obj.CreatedBy = base.UserLoggedIn;
            obj.CreatedDate = DateTime.Now;

            Facade.Save(obj);

            helper.Message = "Data berhasil di simpan !";
            helper.IsSuccess = true;

        }
        catch (Exception ex)
        {
            //throw ex;

            helper.Message = ex.Message;
            helper.IsSuccess = false;

        }

        //return result;
        return Newtonsoft.Json.JsonConvert.SerializeObject(helper);
    }
    

    private string ComboBox(HttpContext context)
    {
        string result = string.Empty;
        try
        {

            List<SysMenu> list = new List<SysMenu>();

            list = Facade.GetList();
            result = new JavaScriptSerializer().Serialize(list);
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }

    private string MenuList(HttpContext context)
    {
        string result = string.Empty;
        try
        {

            result = Facade.RetrieveMenuTree();
        }
        catch (Exception ex)
        {
            throw ex;
        }

        return result;
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}